package com.sttk.fastMark.camera.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.adapter.ColorAdapter;
import com.sttk.fastMark.camera.basic.dialog.BaseSmartDialog;
import com.sttk.fastMark.camera.bean.ColorBean;
import com.sttk.fastMark.camera.bean.ConstructionLogBean;
import com.sttk.fastMark.camera.databinding.DialogEditTemplate2Binding;
import com.sttk.fastMark.camera.utils.AppSpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class EditTemplate2Dialog extends BaseSmartDialog<DialogEditTemplate2Binding> implements View.OnClickListener, OnItemClickListener {

    private ConstructionLogBean constructionLogBean;
    private ColorAdapter colorAdapter;
    private List<ColorBean> listColor;
    private int colorPos;
    private SelTemplateDateDialog selTemplateDateDialog;

    public EditTemplate2Dialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_edit_template2;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.tvDate.setOnClickListener(this);

        // 添加显示/隐藏切换按钮的点击监听器
        mDataBinding.ivShowDate.setOnClickListener(this);
        mDataBinding.ivShowWeather.setOnClickListener(this);
        mDataBinding.ivShowProgress.setOnClickListener(this);
        mDataBinding.ivShowRemark.setOnClickListener(this);
        mDataBinding.ivShowRecorder.setOnClickListener(this);

        mDataBinding.rvColorList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);

        constructionLogBean = AppSpUtils.getConstructionLog();

        mDataBinding.etTitle.setText(constructionLogBean.getTitle());
        mDataBinding.etWeather.setText(constructionLogBean.getWeather());
        mDataBinding.etProgress.setText(constructionLogBean.getProgress());
        mDataBinding.etRemark.setText(constructionLogBean.getRemark());
        mDataBinding.etRecorder.setText(constructionLogBean.getRecorder());

        if (TextUtils.isEmpty(constructionLogBean.getDate())) {
            mDataBinding.tvDate.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
        } else {
            mDataBinding.tvDate.setText(constructionLogBean.getDate());
        }

        // 初始化显示/隐藏状态图标
        mDataBinding.ivShowDate.setImageResource(constructionLogBean.isShowDate() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowWeather.setImageResource(constructionLogBean.isShowWeather() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowProgress.setImageResource(constructionLogBean.isShowProgress() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowRemark.setImageResource(constructionLogBean.isShowRemark() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowRecorder.setImageResource(constructionLogBean.isShowRecorder() ? R.drawable.akai : R.drawable.aguan);

        initColorList();
    }


    private void initColorList() {
        listColor = new ArrayList<>();
        listColor.add(new ColorBean("#000000", false));
        listColor.add(new ColorBean("#D84545", false));
        listColor.add(new ColorBean("#FFA600", false));
        listColor.add(new ColorBean("#FAFD00", false));
        listColor.add(new ColorBean("#13FD00", false));
        listColor.add(new ColorBean("#00F5FF", false));
        listColor.add(new ColorBean("#4B6DFF", false));
        listColor.add(new ColorBean("#FF29FD", false));

        for (int i = 0; i < listColor.size(); i++) {
            ColorBean colorBean = listColor.get(i);
            if (colorBean.getColor().equals(constructionLogBean.getThemeColor())) {
                colorPos = i;
                colorBean.setSelected(true);
                break;
            }
        }
        colorAdapter.setList(listColor);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            saveData();
            dismiss();
            if (listener != null) {
                listener.clickConfirm();
            }
        } else if (id == R.id.tvDate) {//日期选择
            showSelDateDialog();
        } else if (id == R.id.ivShowDate) {//显示/隐藏日期
            constructionLogBean.setShowDate(!constructionLogBean.isShowDate());
            mDataBinding.ivShowDate.setImageResource(constructionLogBean.isShowDate() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowWeather) {//显示/隐藏天气
            constructionLogBean.setShowWeather(!constructionLogBean.isShowWeather());
            mDataBinding.ivShowWeather.setImageResource(constructionLogBean.isShowWeather() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowProgress) {//显示/隐藏进度
            constructionLogBean.setShowProgress(!constructionLogBean.isShowProgress());
            mDataBinding.ivShowProgress.setImageResource(constructionLogBean.isShowProgress() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowRemark) {//显示/隐藏备注
            constructionLogBean.setShowRemark(!constructionLogBean.isShowRemark());
            mDataBinding.ivShowRemark.setImageResource(constructionLogBean.isShowRemark() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowRecorder) {//显示/隐藏记录人
            constructionLogBean.setShowRecorder(!constructionLogBean.isShowRecorder());
            mDataBinding.ivShowRecorder.setImageResource(constructionLogBean.isShowRecorder() ? R.drawable.akai : R.drawable.aguan);
        }
    }

    private void saveData() {
        String title = mDataBinding.etTitle.getText().toString();
        String date = mDataBinding.tvDate.getText().toString();
        String weather = mDataBinding.etWeather.getText().toString();
        String progress = mDataBinding.etProgress.getText().toString();
        String remark = mDataBinding.etRemark.getText().toString();
        String recorder = mDataBinding.etRecorder.getText().toString();

        if (TextUtils.isEmpty(title)) {
            title = "";
        }
        if (TextUtils.isEmpty(weather)) {
            weather = "";
        }
        if (TextUtils.isEmpty(progress)) {
            progress = "";
        }
        if (TextUtils.isEmpty(remark)) {
            remark = "";
        }
        if (TextUtils.isEmpty(recorder)) {
            recorder = "";
        }

        constructionLogBean.setTitle(title);
        constructionLogBean.setDate(date);
        constructionLogBean.setWeather(weather);
        constructionLogBean.setProgress(progress);
        constructionLogBean.setRemark(remark);
        constructionLogBean.setRecorder(recorder);

        AppSpUtils.saveConstructionLog(constructionLogBean);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        colorAdapter.getItem(colorPos).setSelected(false);
        colorPos = position;
        colorAdapter.getItem(position).setSelected(true);
        colorAdapter.notifyDataSetChanged();
        constructionLogBean.setThemeColor(colorAdapter.getItem(position).getColor());
    }

    private void showSelDateDialog() {
        if (selTemplateDateDialog == null) {
            selTemplateDateDialog = new SelTemplateDateDialog(getContext());
            selTemplateDateDialog.setDate(mDataBinding.tvDate.getText().toString());
        }
        selTemplateDateDialog.setListener(new SelTemplateDateDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                mDataBinding.tvDate.setText(TimeUtils.millis2String(date, new SimpleDateFormat("yyyy-MM-dd")));
            }
        });
        selTemplateDateDialog.show();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }
}